package com.itheima.service.impl;

import com.itheima.mapper.DeptMapper;
import com.itheima.pojo.Dept;
import com.itheima.service.DeptService;
import com.itheima.service.EmpService;
import com.itheima.utils.NotNullException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private EmpService empService;
    @Override
    public List<Dept> findALl() {
        List<Dept> list = deptMapper.findAll();
        return list;
    }

    @Override
    public Integer delete(Integer id) {
        Integer count = empService.getDeptId(id);
        if (count!=0){
            throw new NotNullException("对不起，当前部门下有员工，不能直接删除！");
        }
        return deptMapper.delete(id);
    }

    @Override
    public Integer add(Dept dept) {
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        return deptMapper.add(dept);
    }

    @Override
    public Dept getById(Integer id) {
        return deptMapper.getById(id);
    }

    @Override
    public Integer update(Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());
        return deptMapper.update(dept);
    }
}
